import { Component, OnInit, Input, Host } from '@angular/core';
import { Store } from '@ngrx/store';
import { OlState, AddLayer, layerSelector } from '../../../store';

import Extent from 'ol/extent';
import LayerTile from 'ol/layer/tile';
import SourceTile from 'ol/source/tile';

import { MapComponent } from '../../map/map.component';

@Component({
  selector: 'ngx-ol-layer-tile',
  templateUrl: './tile.component.html',
  styleUrls: ['./tile.component.scss']
})
export class TileComponent implements OnInit {

  public instance: LayerTile;

  @Input() opacity: number;
  @Input() preload: number;
  @Input() visible: boolean;
  @Input() extent: Extent;
  @Input() minResolution: number;
  @Input() maxResolution: number;
  @Input() useInterimTilesOnError: boolean;
  @Input() zIndex: number = 1;

  source: SourceTile;

  constructor(
    @Host() private host: MapComponent,
    private store: Store<OlState>
  ) { }

  ngOnInit() {
    this.initialInstance();
  }

  initialInstance(): void {
    this.instance = new LayerTile(this);
    this.host.addLayer(this.instance);
  }

  setSource(source: any): void {
    this.instance.setSource(source);
  }

}
